package com.kkk.shop.user.service.impl;

import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;

import com.alibaba.fastjson.JSONObject;
import com.kkk.shop.common.util.ConstantFinalUtil;
import com.kkk.shop.common.util.PageInfoUtil;
import com.kkk.shop.user.dao.IAdminDao;
import com.kkk.shop.user.dao.IAroleDao;
import com.kkk.shop.user.dao.IAusersAddressDao;
import com.kkk.shop.user.dao.IAusersCarDao;
import com.kkk.shop.user.dao.IAusersDao;
import com.kkk.shop.user.pojo.AAdmins;
import com.kkk.shop.user.pojo.ARole;
import com.kkk.shop.user.pojo.AUsers;
import com.kkk.shop.user.pojo.AUsersAddress;
import com.kkk.shop.user.pojo.AUsersCar;
import com.kkk.shop.user.service.IUserService;

@Service("userService")
public class UserServiceImpl implements IUserService
{
	@Resource
	private IAdminDao adminDao;
	@Resource 
	private IAroleDao roleDao;
	@Resource
	private IAusersAddressDao usersAddressDao;
	@Resource
	private IAusersCarDao usersCarDao;
	@Resource
	private IAusersDao usersDao;
	
	/*  管理员服务实现开始    */
	/**
	 * 插入一条管理员记录
	 * JSONObject 格式{code:"1",info:"添加成功",data:{effectRows:"11","id":"11"}}
	 * */
	@Override
	public JSONObject insertOneAdminService(AAdmins admin)
	{
		this.resultJson.clear();
		int res=this.adminDao.insertOne(admin);
//		String str=null;
//		if(str.equals("aa"))
//		{
//		}
		if(res>0)
		{
			this.resultJson.put("code", "0");
			this.resultJson.put("info", ConstantFinalUtil.INFO_JSON.get("0"));
			this.dataJson.clear();
			this.dataJson.put("id", admin.getId());
			this.dataJson.put("effectRows", res);
			this.resultJson.put("data", this.dataJson);
		}else
		{
			this.resultJson.put("code", "1");
			this.resultJson.put("info", ConstantFinalUtil.INFO_JSON.get("1"));
		}
		return this.resultJson;
	}

	@Override
	public JSONObject updateOneAdminService(AAdmins admin)
	{
		this.resultJson.clear();
		int res=this.adminDao.updateOne(admin);
		if(res>0)
		{
			this.resultJson.put("code", "0");
			this.resultJson.put("info", ConstantFinalUtil.INFO_JSON.get("0"));
			this.dataJson.clear();
			this.dataJson.put("id", admin.getId());
			this.dataJson.put("effectRows", res);
			this.resultJson.put("data", this.dataJson);
		}else
		{
			this.resultJson.put("code", "2");
			this.resultJson.put("info", ConstantFinalUtil.INFO_JSON.get("2"));
		}
		return this.resultJson;
	}

	@Override
	public JSONObject deleteOneAdminService(AAdmins admin)
	{
		this.resultJson.clear();
		int res=this.adminDao.deleteOne(admin);
		if(res>0)
		{
			this.resultJson.put("code", "0");
			this.resultJson.put("info", ConstantFinalUtil.INFO_JSON.get("0"));
			this.dataJson.clear();
			this.dataJson.put("id", admin.getId());
			this.dataJson.put("effectRows", res);
			this.resultJson.put("data", this.dataJson);
		}else
		{
			this.resultJson.put("code", "3");
			this.resultJson.put("info", ConstantFinalUtil.INFO_JSON.get("3"));
		}
		return this.resultJson;
	}

	@Override
	public AAdmins queryOneAdminsService(Map<String, Object> paramMap)
	{
		return this.adminDao.queryOne(paramMap);
	}

	@Override
	public List<AAdmins> queryMultiAdminsService(PageInfoUtil pageInfoUtil, Map<String, Object> paramMap)
	{
		//模糊查询的拼接由后台进行控制,避免用户乱输入
		if(paramMap.get("keywords")!=null && !"".equalsIgnoreCase(paramMap.get("keywords") + ""))
		{
			paramMap.put("keywords","%"+paramMap.get("keywords")+"%");
		}
		//进行分页
		if(pageInfoUtil!=null)
		{
			//先查询全部记录
			paramMap.put("pageCond", "true");
			List<AAdmins> adminList=this.adminDao.queryMulti(paramMap);
			//查出总记录数
			if(adminList.size()==1)
			{
				//总记录数已经存放在id中
				pageInfoUtil.setTotalRecord(adminList.get(0).getId());
			}
			paramMap.put("pageCond", "false");
			paramMap.put("currentRecord", pageInfoUtil.getCurrentRecord());
			paramMap.put("pageSize", pageInfoUtil.getPageSize());
			return this.adminDao.queryMulti(paramMap);
		}
			return this.adminDao.queryMulti(paramMap);
	}
	/*  管理员服务实现结束   */

	/* 角色服务实现开始  */
	@Override
	public JSONObject insertOneAroleService(ARole role)
	{
		this.resultJson.clear();
		int res=this.roleDao.insertOne(role);
//		String str=null;
//		if(str.equals("aa"))
//		{
//		}
		if(res>0)
		{
			this.resultJson.put("code", "0");
			this.resultJson.put("info", ConstantFinalUtil.INFO_JSON.get("0"));
			this.dataJson.clear();
			this.dataJson.put("id", role.getId());
			this.dataJson.put("effectRows", res);
			this.resultJson.put("data", this.dataJson);
		}else
		{
			this.resultJson.put("code", "1");
			this.resultJson.put("info", ConstantFinalUtil.INFO_JSON.get("1"));
		}
		return this.resultJson;
	}

	@Override
	public JSONObject updateOneAroleService(ARole role)
	{
		this.resultJson.clear();
		int res=this.roleDao.updateOne(role);
		if(res>0)
		{
			this.resultJson.put("code", "0");
			this.resultJson.put("info", ConstantFinalUtil.INFO_JSON.get("0"));
			this.dataJson.clear();
			this.dataJson.put("id", role.getId());
			this.dataJson.put("effectRows", res);
			this.resultJson.put("data", this.dataJson);
		}else
		{
			this.resultJson.put("code", "2");
			this.resultJson.put("info", ConstantFinalUtil.INFO_JSON.get("2"));
		}
		return this.resultJson;
	}

	@Override
	public JSONObject deleteOneAroleService(ARole role)
	{
		this.resultJson.clear();
		int res=this.roleDao.deleteOne(role);
		if(res>0)
		{
			this.resultJson.put("code", "0");
			this.resultJson.put("info", ConstantFinalUtil.INFO_JSON.get("0"));
			this.dataJson.clear();
			this.dataJson.put("id", role.getId());
			this.dataJson.put("effectRows", res);
			this.resultJson.put("data", this.dataJson);
		}else
		{
			this.resultJson.put("code", "3");
			this.resultJson.put("info", ConstantFinalUtil.INFO_JSON.get("3"));
		}
		return this.resultJson;
	}

	@Override
	public ARole queryOneAroleService(Map<String, Object> paramMap)
	{
		return this.roleDao.queryOne(paramMap);
	}

	@Override
	public List<ARole> queryMultiAroleService(PageInfoUtil pageInfoUtil, Map<String, Object> paramMap)
	{
		//模糊查询的拼接由后台进行控制,避免用户乱输入
		if(paramMap.get("keywords")!=null && !"".equalsIgnoreCase(paramMap.get("keywords") + ""))
		{
			paramMap.put("keywords","%"+paramMap.get("keywords")+"%");
		}
		//进行分页
		if(pageInfoUtil!=null)
		{
			//先查询全部记录
			paramMap.put("pageCond", "true");
			List<ARole> adminList=this.roleDao.queryMulti(paramMap);
			//查出总记录数
			if(adminList.size()==1)
			{
				//总记录数已经存放在id中
				pageInfoUtil.setTotalRecord(adminList.get(0).getId());
			}
			paramMap.put("pageCond", "false");
			paramMap.put("currentRecord", pageInfoUtil.getCurrentRecord());
			paramMap.put("pageSize", pageInfoUtil.getPageSize());
			return this.roleDao.queryMulti(paramMap);
		}
			return this.roleDao.queryMulti(paramMap);
	}
	/* 角色服务实现结束   */

	@Override
	public JSONObject insertOneAUsersAddressService(AUsersAddress usersAddress)
	{
		this.resultJson.clear();
		//操作后的影响记录数
		int res=this.usersAddressDao.insertOne(usersAddress);
		if(res>0)
		{
			resultJson.put("code","0");
			resultJson.put("info",ConstantFinalUtil.INFO_JSON.getString("0"));
			this.dataJson.clear();
			this.dataJson.put("id",usersAddress.getId()+"");
			this.dataJson.put("effectRows",res+"");
			this.resultJson.put("data",this.dataJson);
		}else
		{
			this.resultJson.put("code","1");
			this.resultJson.put("info", ConstantFinalUtil.INFO_JSON.getString("1"));
		}
		return this.resultJson;
	}

	@Override
	public JSONObject updateOneAUsersAddressService(AUsersAddress usersAddress)
	{
		this.resultJson.clear();
		int res=this.usersAddressDao.updateOne(usersAddress);
		if(res>0)
		{
			this.resultJson.put("code","0");
			this.resultJson.put("info",ConstantFinalUtil.INFO_JSON.getString("0"));
			this.dataJson.clear();
			this.dataJson.put("id",usersAddress.getId()+"");
			this.dataJson.put("effectRows",res+"");
			this.resultJson.put("data",this.dataJson);
		}else
		{
			this.resultJson.put("code","2");
			this.resultJson.put("info",ConstantFinalUtil.INFO_JSON.getString("2"));
		}
		return this.resultJson;
	}

	@Override
	public JSONObject deleteOneAUsersAddressService(AUsersAddress usersAddress)
	{
		this.resultJson.clear();
		int res=this.usersAddressDao.deleteOne(usersAddress);
		if(res>0)
		{
			this.resultJson.put("code","0");
			this.resultJson.put("info",ConstantFinalUtil.INFO_JSON.getString("0"));
			//存储data
			this.dataJson.clear();
			this.dataJson.put("id",usersAddress.getId()+"");
			this.dataJson.put("effectRows",res+"");
			this.resultJson.put("data",this.dataJson);
		}else
		{
			this.resultJson.put("code","3");
			this.resultJson.put("info",ConstantFinalUtil.INFO_JSON.getString("3"));
		}
		return this.resultJson;
	}

	@Override
	public AUsersAddress queryOneAUsersAddressService(Map<String, Object> paramMap)
	{
		return this.usersAddressDao.queryOne(paramMap);
	}

	@Override
	public List<AUsersAddress> queryMultiAUsersAddressService(PageInfoUtil pageInfoUtil, Map<String, Object> paramMap)
	{
		//查询之前先处理关键字
		if(paramMap.get("keywords")!=null &&!"".equalsIgnoreCase(paramMap.get("keywords")+""))
		{
			paramMap.put("keywords","%"+paramMap.get("keywords")+"%");
		}
		if(pageInfoUtil!=null)
		{
			//分页之前先查询总记录书
			paramMap.put("pageCond","true");
			List<AUsersAddress> usersAddressesList=this.usersAddressDao.queryMulti(paramMap);
			//由于字段存在id中,因此只需要取出第一个就行
			if(usersAddressesList.size()==1)
			{
				pageInfoUtil.setTotalRecord(usersAddressesList.get(0).getId());
			}
			//真正开始查询分页的数据
			paramMap.put("pageCond","false");
			paramMap.put("currentRecord",pageInfoUtil.getCurrentPage());
			paramMap.put("pageSize",pageInfoUtil.getPageSize());
			return this.usersAddressDao.queryMulti(paramMap);
		}
		//不进行分页,直接进行查询
		return this.usersAddressDao.queryMulti(paramMap);
	}

	@Override
	public JSONObject insertOneAUsersService(AUsers users)
	{
		this.resultJson.clear();
		int res=this.usersDao.insertOne(users);
		if(res>0)
		{
			this.resultJson.put("code","0");
			this.resultJson.put("info",ConstantFinalUtil.INFO_JSON.getString("0"));
			this.dataJson.clear();
			this.dataJson.put("id",users.getId()+"");
			this.dataJson.put("effectRows",res+"");
			this.resultJson.put("data",this.dataJson);
		}else
		{
			this.resultJson.put("code","1");
			this.resultJson.put("info",ConstantFinalUtil.INFO_JSON.getString("1"));
		}
		return this.resultJson;
	}

	@Override
	public JSONObject updateOneAUsersService(AUsers users)
	{
		this.resultJson.clear();
		int res=this.usersDao.updateOne(users);
		if(res>0)
		{
			this.resultJson.put("code","0");
			this.resultJson.put("info", ConstantFinalUtil.INFO_JSON.getString("0"));
			this.dataJson.clear();
			this.dataJson.put("id",users.getId()+"");
			this.dataJson.put("effectRows",res+"");
			this.resultJson.put("data",this.dataJson);
		}else
		{
			this.resultJson.put("code","2");
			this.resultJson.put("info",ConstantFinalUtil.INFO_JSON.getString("2"));
		}
		return this.resultJson;
	}

	@Override
	public JSONObject deleteOneAUsersService(AUsers users)
	{
		this.resultJson.clear();
		int res=this.usersDao.deleteOne(users);
		if(res>0)
		{
			this.resultJson.put("code","0");
			this.resultJson.put("info",ConstantFinalUtil.INFO_JSON.getString("0"));
			this.dataJson.clear();
			this.dataJson.put("id",users.getId()+"");
			this.dataJson.put("effectRows",res+"");
			this.resultJson.put("data",this.dataJson);
		}else
		{
			this.resultJson.put("code", "3");
			this.resultJson.put("info", ConstantFinalUtil.INFO_JSON.getString("3"));
		}
		return this.resultJson;
	}

	@Override
	public AUsers queryOneAUsersService(Map<String, Object> paramMap)
	{
		return this.usersDao.queryOne(paramMap);
	}

	@Override
	public List<AUsers> queryMultiAUsersService(PageInfoUtil pageInfoUtil, Map<String, Object> paramMap)
	{
		//执行前先将keywords转换成模糊查询的条件
		if(paramMap.get("keywords")!=null && !"".equalsIgnoreCase(paramMap.get("keywords")+""))
		{
			paramMap.put("keywords","%"+paramMap.get("keywords")+"%");
		}
		if(pageInfoUtil!=null)
		{
			//进行分页之前,先查询总记录数,此处将总记录数存放在id中
			paramMap.put("pageCond","true");
			List<AUsers> usersList=this.usersDao.queryMulti(paramMap);
			//如果size==1代表查询到总记录数
			if(usersList.size()==1)
			{
				pageInfoUtil.setTotalRecord(usersList.get(0).getId());
			}
			paramMap.put("pageCond","false");
			paramMap.put("currentRecord",pageInfoUtil.getCurrentRecord());
			paramMap.put("pageSize",pageInfoUtil.getPageSize());
			return this.usersDao.queryMulti(paramMap);
		}
		return this.usersDao.queryMulti(paramMap);
	}

	@Override
	public JSONObject insertOneAUsersCarService(AUsersCar usersCar)
	{
		this.resultJson.clear();
		int res=this.usersCarDao.insertOne(usersCar);
		if(res>0)
		{
			this.resultJson.put("code","0");
			this.resultJson.put("info",ConstantFinalUtil.INFO_JSON.getString("0"));
			this.dataJson.clear();
			this.dataJson.put("id",usersCar.getId()+"");
			this.dataJson.put("effectRows",res+"");
			this.resultJson.put("data",this.dataJson);
		}else
		{
			this.resultJson.put("code","1");
			this.resultJson.put("info",ConstantFinalUtil.INFO_JSON.getString("1"));
		}
		return this.resultJson;
	}

	@Override
	public JSONObject updateOneAUsersCarService(AUsersCar usersCar)
	{
		this.resultJson.clear();
		int res=this.usersCarDao.updateOne(usersCar);
		if(res>0)
		{
			this.resultJson.put("code","0");
			this.resultJson.put("info",ConstantFinalUtil.INFO_JSON.getString("0"));
			this.dataJson.clear();
			this.dataJson.put("id",usersCar.getId()+"");
			this.dataJson.put("effectRows",res+"");
			this.resultJson.put("data",this.dataJson);
		}else
		{
			this.resultJson.put("code","1");
			this.resultJson.put("info",ConstantFinalUtil.INFO_JSON.getString("1"));
		}
		return this.resultJson;
	}

	@Override
	public JSONObject deleteOneAUsersCarService(AUsersCar usersCar)
	{
		this.resultJson.clear();
		int res=this.usersCarDao.deleteOne(usersCar);
		if(res>0)
		{
			this.resultJson.put("code","0");
			this.resultJson.put("info",ConstantFinalUtil.INFO_JSON.getString("0"));
			this.dataJson.clear();
			this.dataJson.put("id",usersCar.getId()+"");
			this.dataJson.put("effectRows",res+"");
			this.resultJson.put("data",this.dataJson);
		}else
		{
			this.resultJson.put("code","1");
			this.resultJson.put("info",ConstantFinalUtil.INFO_JSON.getString("1"));
		}
		return this.resultJson;
	}

	@Override
	public AUsersCar queryOneAUsersCarService(Map<String, Object> paramMap)
	{
		return this.usersCarDao.queryOne(paramMap);
	}

	@Override
	public List<AUsersCar> queryMultiAUsersCarService(PageInfoUtil pageInfoUtil, Map<String, Object> paramMap)
	{
		//查询之前先对keywords进行处理
		if(paramMap.get("keywords")!=null && !"".equalsIgnoreCase(paramMap.get("keywords")+""))
		{
			//中英文%要区别
			paramMap.put("keywords","%"+paramMap.get("keywords")+"%");
		}
		if(pageInfoUtil!=null)
		{
			//进行分页查询先查询总记录数
			paramMap.put("pageCond", "true");
			List<AUsersCar> userCarList=this.usersCarDao.queryMulti(paramMap);
			if(userCarList.size()==1)
			{
				//将存在id中的总记录数取出
				pageInfoUtil.setTotalRecord(userCarList.get(0).getId());
			}
			paramMap.put("pageCond", "false");
			paramMap.put("currentRecord",pageInfoUtil.getCurrentRecord());
			paramMap.put("pageSize",pageInfoUtil.getPageSize());
			return this.usersCarDao.queryMulti(paramMap);
		}
		return this.usersCarDao.queryMulti(paramMap);
	}
}
